clear
version 14.0
set more off
set scheme s2mono


* Specify the year of cash rent data to use in the regression
local reg_year 2013

/*
cd ../maps/
shp2dta using stfips_clust, ///
		database(stfips_clust_db) coordinates(stfips_clust_coord) genid(id) gencentroids(s) replace
*/

cd ../codeAnalysis/
use ../maps/stfips_clust_db, clear
rename stfip stfips_clust
spmat contiguity cstate using ../maps/stfips_clust_coord, id(id) normalize(minmax) replace
spmat idistance dstate x_s y_s, id(id) normalize(minmax) replace
spmat summarize dstate

* put spatial weight matrix into a format to be able to calculate Moran's I
spmat export dstate using "../temp/dstate.txt", noid replace
insheet using "../temp/dstate.txt", delim(" ") clear
drop in 1
save "../temp/dstate.dta", replace
set matsize 1000
spatwmat using "../temp/dstate.dta", name(dstateWs) standardize


*capture log close
*log using "..\logs\main results `reg_year'.txt", text replace

* run the do-files to define programs used in this code
run "..\codeSetup\soils transform - program.do"
run "main results - program.do"

local bootstrap_reps 1000

*------------------------------------------------------------------------
* Potential soils
*------------------------------------------------------------------------
local soils  slope om sar gypsum cec7 ec  ksat clay_perc silt_perc ///
			bulkDensity soc0_150 rootznemc ph_less6 ph_greater7dot5 hydgrp* text_*

local soils_nonlin  slope_* om_* sar_* gypsum_* cec7_* ec_*  ksat_* clay_perc_* silt_perc_* ///
			bulkDensity_* soc0_150_* rootznemc_* ph_less6 ph_greater7dot5 hydgrp* text_*

*------------------------------------------------------------------------
* Controls to include in the regression
*------------------------------------------------------------------------
local deficit water_deficit
local surplus water_surplus
local edd dday34C
local soils_controls soc0_150_4 bulkDensity_4 ec_2 ph_less6 ph_greater7dot5 slope_1     
*local soils_controls $SelSoils

local knots_`deficit' 2
local knots_`surplus' 2
local knots_gdd 3
local knots_`edd' 3

local main_year 2013

*------------------------------------------------------------------------
* Regression
*------------------------------------------------------------------------

use "..\dataAnalysis\rent_climate_data", clear
gen ln_cash_rent=ln(cash_rent_non`reg_year')
rename stfips stfip
rename cofips cofip
merge 1:1 stfip cofip using "../maps/county_db_my_region2", keep(match) nogen
rename stfip stfips
rename cofip cofips

capture drop `deficit'_spline*
gen `deficit'_spline = `deficit'
matrix `deficit'_knots=[0]

capture drop water_surplus_spline*
gen `surplus'_spline = `surplus'
matrix `surplus'_knots=[0]

capture drop gdd_spline*
mkspline gdd_spline = gdd, cubic nknots(`knots_gdd') 
matrix gdd_knots=r(knots)

capture drop `edd'_spline*
mkspline `edd'_spline = `edd', cubic nknots(`knots_`edd'') 
matrix `edd'_knots=r(knots)		


summ `deficit'_spline* `surplus'_spline* gdd_spline* `edd'_spline* `soils_controls' stfips_clust croplandNon_acres 

drop if ln_cash_rent==.
drop if croplandNon_acres==.
tab stfips, gen(stfe)
summ `deficit'_spline* `surplus'_spline* gdd_spline* `edd'_spline* `soils_controls'	


reg ln_cash_rent `deficit'_spline* `surplus'_spline* gdd_spline* `edd'_spline* `soils_controls' i.stfips ///
			[aw=croplandNon_acres]
estat hettest
predict ehat, resid

collapse ehat, by(stfips_clust)
spatgsa ehat, weights(dstateWs) moran

